package Tools;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTParser;
import org.eclipse.jdt.core.dom.ASTVisitor;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.MethodDeclaration;



public class Parser{
	
	private class ASTMethodVisitor extends ASTVisitor{

		private String methodName;
		private String result = null;
		
		public ASTMethodVisitor(String methodName){
			this.methodName = methodName;
		}
		
		public String getResult(){
			return result;
		}
		
		@Override
		public boolean visit(MethodDeclaration node){
			if (node.getName().toString().equals(methodName)){
				result = node.toString();
				return true;
			}
			return false;
		}
	}
	
	public String parse(String source, String methodName){
		
		ASTParser parser = ASTParser.newParser(AST.JLS4);
		parser.setSource(source.toCharArray());
		parser.setKind(ASTParser.K_COMPILATION_UNIT);

		CompilationUnit cu = (CompilationUnit) parser.createAST(null);
		ASTMethodVisitor visitor = new ASTMethodVisitor(methodName);
		cu.accept(visitor);
		return visitor.getResult();
	}

	// read file content into a string
	public static String readFileToString(String filePath) throws IOException {
		StringBuilder fileData = new StringBuilder();
		BufferedReader reader = new BufferedReader(new FileReader(filePath));
		char[] buf = new char[128];
		int numRead = 0;
		while ((numRead = reader.read(buf)) != -1) {
			String readData = String.valueOf(buf, 0, numRead);
			fileData.append(readData);
		}
		reader.close();
		return  fileData.toString();	
	}
	
	/*	private String fileName;
	private String directory;

	public Parser(String filename, String directory){
		this.fileName = filename;
		this.directory = directory;

	}

	public String parseAndFind(Method method){
		//String visibility = method.get
		String methodName = method.getName();
		Class returnType = method.getReturnType();
		Class[] parameterTypes = method.getParameterTypes();



		String chaine = "";

		InputStream ips = null;
		try {
			ips = new FileInputStream(directory+fileName);
		} catch (FileNotFoundException e1) {
			e1.printStackTrace();
		} 
		InputStreamReader ipsr = new InputStreamReader(ips);
		BufferedReader br = new BufferedReader(ipsr);
		String ligne;

		try {
			while ((ligne = br.readLine()) != null){
				//System.out.println(ligne);
				chaine+=ligne+"\n";
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		try {
			br.close();
		} catch (IOException e) {
			e.printStackTrace();
		} 



		String regex = returnType.getSimpleName()+"\\s+"+methodName+"\\s+(\\s+";
		for (Class paramater : parameterTypes){
			regex += paramater.getSimpleName()+"\\s+";
		}
		regex += ")";

		//System.out.println("String\s+toString\s*\();


		Pattern pattern = Pattern.compile("String\\s+toString\\s*\\(");
		Matcher matcher = pattern.matcher(chaine);
		matcher.find();
		String texte = matcher.group();
        int debut = matcher.start();
        int fin = matcher.end();

       // System.out.println(texte+" "+debut+" "+fin);

		return "";
	}*/
}
